const { HttpRequest, globalData, UserAccess, addUserStat } = getApp();
Page({
  data: {
    loadingActive: false,
    activeWrap: false,
    currentPage: 1,
    pageSize: 10,
    taskList: [],
    router: '/pages/task-page/task-page',
    pagetitle: '积分列表',
    loadingActive: false
  },
  onLoad() {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    this.initPage()
    let routerArr = getCurrentPages();
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
    }
    let track = UserAccess(UserAccessoptions)


    // 自定义埋点
    let customAccessoptions = {
      customType: '历史积分'
    }
    console.log('历史积分');


    let tracks = addUserStat(customAccessoptions)
  },
  onReachBottom() {

  },
  onaTouchMove(event) {
  },
  async initPage() {
    let { pageSize, currentPage, taskList, nextId } = this.data;
    let bodyObjtack = {
      "brandPointSign": "CNVansPoint",   //品牌积分标记，扩展值默认
      "param": globalData.ouid,
      "type": 3,   // Type决定了param的值。  0:表示param是手机 1:表示param是会员ID  3:表示param是Taobao_Nick
      "operationType": 0,
      "beginTime": "",
      "endTime": "",
      "nextId": currentPage,
      "pageSize": pageSize   //必填
    }
    let pathtack = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/query`;
    // this.userInfo()
    this.setData({
      loadingActive: true
    })
    let restack = await HttpRequest(pathtack, 'POST', '', bodyObjtack);
    console.log(restack, '222222222222222222222222222');

    //积分查询 
    let bodyObj2 = {
      // "param": "13800138000",
      // "type": 0,
      "param": globalData.ouid,
      "type": 3
    }
    let path2 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/account`;
    let res2 = await HttpRequest(path2, 'POST', '', bodyObj2);
    console.log(res2.data , res2.data.data.availablePoint , '11111111111111111111111111111111111111');


    if (res2.data.resultCode == 0) {
      globalData.userInfo.availablePoint = res2.data.data.availablePoint;
      globalData.userInfo.willExpirePoint = res2.data.data.willExpirePoint; 

      this.setData({
        menuavailablePoint: res2.data.data.availablePoint,
        menuwillExpirePoint: res2.data.data.willExpirePoint
      })
    }
    this.setData({
      loadingActive: false
    })
    let time = new Date().getTime()
    console.log(restack.data.resultCode, '333333333333');
    console.log(restack.data.data, '4444444444444444');
    if (restack.data.resultCode == 0 && restack.data.data.list) { 
      let arr = restack.data.data.list;
      // console.log(arr, '===============')
      arr.forEach(item => {
        if (item.invalidTime) {
          let invalid = new Date(item.invalidTime).getTime() - time;//0已失效1即将失效2正常
          if (typeof (item.staus) != 'number') {
            //状态，一个月以内即将失效  > 60 * 60 * 24 * 30 * 1000 =>正常   <0失效       1即将失效0已失效
            item.staus = (invalid) > 60 * 60 * 24 * 30 * 1000 ? 2 : (invalid) <= 0 ? 0 : 1;
          }
        } else {
          item.staus = 2;
        }

        // console.log(item.staus, '===================', invalid, new Date(item.invalidTime).getTime(), '=======================', time)
        // let y = new Date(item.invalidTime).getFullYear();
        // let m = new Date(item.invalidTime).getMonth() + 1;
        // (m / 10 > 1) ? m = m : m = '0' + m;
        // let d = new Date(item.invalidTime).getDate();
        // (d / 10 > 1) ? d = d : d = '0' + d;
        // item.invalidTime = `${y}.${m}.${d}`;
        item.validTime = item.validTime.substring(0, 10).replace(/\//g, '.')
      });
      // taskList = Object.assign(taskList,arr);
      taskList = taskList.concat(arr)
      if (restack.data.data.list.length > 0) {
        currentPage = currentPage + 1
      }
      // console.log(taskList,'===========================',arr)
    }
    // my.alert({
    //   content: taskList
    // })
    this.setData({
      taskList,
      loadingActive: false,
      currentPage,
      menuuserInfo: globalData.userInfo,
      menuavailablePoint: globalData.userInfo.availablePoint,
      menuwillExpirePoint: globalData.userInfo.willExpirePoint
    })


  },
  navChange() {
    this.setData({
      activeWrap: true
    })
  },
  navExit() {
    this.setData({
      activeWrap: false
    })
  },
  queryFn() {
    this.setData({
      loadingActive: true
    })

    this.initPage()
  },
  wangwang() {
    my.tb.openMessage({
      sellerNick: "vans官方旗舰店",
      success: (res) => {
      },
      fail: (res) => {
      },
    })

    // 自定义埋点
    let customAccessoptions = {
      customType: '联系客服'
    }
    console.log('联系客服');

    let tracks = addUserStat(customAccessoptions)
  }
});
